<?php
/**
 * Creater: 李青林
 * Date Time : 2018/10/22 17:52
 */

namespace App\Admin\Controllers;

use App\Http\Models\Goods;
use App\Http\Models\GoodsCoupon;
use App\Http\Models\GoodsCorrection;
use App\Http\Models\GoodsSales;
use App\Http\Models\GoodsCategory;
use App\Http\Models\TaobaoCategory;
use DB;
use Illuminate\Support\Facades\Cache;
use Encore\Admin\Auth\Permission;
use Encore\Admin\Facades\Admin;
use App\Http\Controllers\Controller;

class ApiController extends Controller
{
    /**
     * 对商品进行审核
     */
    public function checkGood()
    {
        $gid = $this->request->gid;
        $failReason = $this->request->failReason;
        $fail_content = $this->request->fail_content;
        $integralReason = $this->request->integralReason;
        $goodsstate = $this->request->goodsstate;
        $goods = Goods::where(['id' => $gid])->first();
        $old_status = $goods->goodsstate;
        if (($goods->goodstype == 0 && Admin::user()->isRole('goods')) || ($goods->goodstype == 1 && Admin::user()->isRole('bursting')) || ($goods->goodstype == 2 && Admin::user()->isRole('kill'))) {
            if ($goods) {
                //审核
                if (($goods->goodsstate == 0 && in_array($goodsstate, [2, 3])) || ($goods->goodsstate == 2 && $goodsstate == 4)) {
                    /*$good = Goods::where(['goodsid' => $goods->goodsid, 'goodsstate' => 2])->first();
                    if ($goodsstate == 2 && $good) {
                        return json_encode(['code' => 1, 'msg' => '该商品线上已存在,不能审核通过!', 'data' => '']);
                    }*/
                    $goods->goodsstate = $goodsstate;
                    $g_bool = $goods->save();
                    $log_bool = Goods::save_log($gid, $goodsstate, $failReason, $integralReason, $old_status, $fail_content, 1);
                    if ($g_bool && $log_bool) {
                        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => '']);
                    }
                    return json_encode(['code' => 1, 'msg' => '修改状态失败!', 'data' => '']);
                } else {
                    return json_encode(['code' => 1, 'msg' => '操作有误!', 'data' => '']);
                }
            }
        }
        return json_encode(['code' => 1, 'msg' => '暂无权限操作', 'data' => '']);
    }

    public function showReason()
    {
        $gid = $this->request->gid;
        $goods = Goods::select('goodstype')->where(['id' => $gid])->first();
        if (empty($goods)) {
            return json_encode(['code' => 1, 'msg' => '不存在商品', 'data' => '']);
        }
        $failReason = Goods::getFailReason($goods->goodstype);
        $integralReason = Goods::getIntegral();
        //dd($failReason);
        return view('admin.reason', compact('failReason', 'integralReason'))->with(['gid' => $gid]);
    }

    /**
     * 获取商品的主图
     * @return string
     */
    public function getGoodsCoverimage()
    {
        $gid = $this->request->gid;
        $arr = [];
        if (empty($gid)) {
            return json_encode(['code' => 1, 'msg' => 'ng', 'data' => $arr]);
        }
        $goods = Goods::select('coverimage')->where(['id' => $gid])->first();
        if (empty($goods)) {
            return json_encode(['code' => 1, 'msg' => 'ng', 'data' => $arr]);
        }
        foreach ($goods->coverimage as $k => $value) {
            $arr[] = ['title' => $k, 'content' => $value];
        }
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $arr]);
    }

    /**
     * 进行设置商品的主图
     * @return string
     */
    public function setGoodImage()
    {
        $gid = $this->request->gid;
        $number = $this->request->number;

        if (empty($gid) || empty($number)) {
            return json_encode(['code' => 1, 'msg' => 'ng', 'data' => '']);
        }
        $goods = DB::table('goods')->where(['id' => $gid])->first();
        $coverimage = json_decode($goods->coverimage, true);
        $tmp = $coverimage['main'];
        $coverimage['main'] = $coverimage[$number];
        $coverimage[$number] = $tmp;
        $bool = DB::table('goods')->where(['id' => $gid])->update(['coverimage' => json_encode($coverimage, 320), 'img_status' => 1]);
        if ($bool) {
            return json_encode(['code' => 0, 'msg' => 'ok', 'data' => '']);
        } else {
            return json_encode(['code' => 1, 'msg' => 'ng', 'data' => '']);
        }
    }


    /**
     * 处理纠错
     */
    public function error_correction()
    {
        $id = $this->request->id;
        if (empty($id)) {
            return json_encode(['code' => 1, 'msg' => '商品id没有传递',]);
        }
        $goods_error = GoodsCorrection::find($id);
        if (empty($goods_error)) {
            return json_encode(['code' => 1, 'msg' => '不存在该商品错误记录',]);
        }
        $type = [$goods_error->type];
        $bool = GoodsCorrection::updateErrorStatus($goods_error->gid, $type);
        if ($bool) {
            return json_encode(['code' => 0, 'msg' => '已修改']);
        } else {
            return json_encode(['code' => 1, 'msg' => '修改失败']);
        }

    }

    /**
     * 删除该错误日志记录
     */
    public function del_error_goods()
    {
        $id = $this->request->id;
        if (empty($id)) {
            return json_encode(['code' => 1, 'msg' => '商品id没有传递',]);
        }
        $goods_error = GoodsCorrection::find($id);
        if (empty($goods_error)) {
            return json_encode(['code' => 1, 'msg' => '不存在该商品错误记录',]);
        }
        $goods_error->deleted_at = date('Y-m-d H:i:s', time());
        $bool = $goods_error->save();
        if ($bool) {
            return json_encode(['code' => 0, 'msg' => '删除成功!']);
        } else {
            return json_encode(['code' => 1, 'msg' => '删除失败!']);
        }
    }

}